/**
 * @Author：ming
 * @Date：2023/10/25 7:23 PM
 * @Filename：LC49
 */

import java.util.*;

/**
 * @author ming
 */
public class LC49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> lists = new ArrayList<>();
        int len = strs.length;
        HashMap<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            char[] c = strs[i].toCharArray();
            Arrays.sort(c);
            String s = new String(c);
            if(map.containsKey(s)){
                List<String> list = map.get(s);
                list.add(strs[i]);
            }else {
                ArrayList<String> list = new ArrayList<>();
                list.add(strs[i]);
                map.put(s,list);
            }
        }
        Set<String> keySet = map.keySet();
        for (String s : keySet) {
            lists.add(map.get(s));
        }
        return lists;
    }
}
